package com.woniuxy.controller;

import com.woniuxy.pojo.Customer;
import com.woniuxy.pojo.ServiceManager;
import com.woniuxy.pojo.User;
import com.woniuxy.service.CustomerService;
import com.woniuxy.service.MyLog;
import com.woniuxy.service.ServiceService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import com.woniuxy.vo.ServiceVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
public class ServiceController extends BaseController {
    @Autowired
    ServiceService serviceService;
    @Autowired
    CustomerService customerService;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 进行创建服务单操作
     * 数据进来的时候已经包含servTitle，servCustNo，servRequest，servType
     * 其他必须参数由后端绑定：servNo，servCustName，servStatus，servCreateBy，servCreateDate，servFlag（默认为1）
     *
     * @param serviceManager
     * @param request
     * @return
     */
    @MyLog(value = "创建服务单")  //这里添加了AOP的自定义注解
    @PostMapping("/service.do")
    public ResultDto newWorkOrder(ServiceManager serviceManager, HttpServletRequest request) {
        logger.info(serviceManager.getServCustNo());
        logger.info(serviceManager.getServRequest());
        logger.info(serviceManager.toString());
        User user = (User) request.getSession().getAttribute("USER_SESSION");
        serviceManager.setServCreateBy(user.getRealname());
        Date date = new Date();
        serviceManager.setServNo("SE" + date.getTime());
        Customer customer = customerService.selectByid(serviceManager.getServCustNo());
        serviceManager.setServCustName(customer.getCustName());
        serviceManager.setServStatus(1);
        serviceManager.setServCreateDate(date);
        serviceManager.setServFlag(1);
        logger.info(serviceManager.toString());
        serviceManager.setServNo("SE" + (new Date().getTime()));
        boolean flag = serviceService.newWorkOrder(serviceManager);
        if (flag) {
            return ResultDto.NEW_SERVICE_ORDER_SUCCESS;
        } else {
            return ResultDto.NEW_SERVICE_ORDER_FAILURE;
        }

    }

    @GetMapping("/services")
    public DataGridView selectService(ServiceVo serviceVo) {
        logger.info("当前页数" + serviceVo.getPage());
        logger.info("当前类型：" + serviceVo.getServType());
        logger.info("当前条数" + serviceVo.getLimit());
        DataGridView d = serviceService.selectServiceByServiceVo(serviceVo);
        logger.info(d.getData().toString());
        return d;
    }

    @GetMapping("/services/{reanNames}")
    public DataGridView selectServiceManagerByName(@PathVariable("reanNames") String reanNames) {
        ServiceVo serviceVo = new ServiceVo();
        serviceVo.setRealName(reanNames);
        return serviceService.selectServiceByServiceVo(serviceVo);
    }

    @GetMapping("/services/distribute.do")
    public ResultDto distributeService(String servNo, String username) {
        HashMap<String, String> map = new HashMap<>();
        map.put("servNo", servNo);
        map.put("username", username);

        try {
            serviceService.distributeService(map);
            return ResultDto.DISTRIBUTE_SERVICE_ORDER_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.DISTRIBUTE_SERVICE_ORDER_FAILURE;
        }
    }
    @MyLog(value = "服务单处理")  //这里添加了AOP的自定义注解
    @PostMapping("/serviceDeal.do")
    public ResultDto serviceDeal(ServiceManager serviceManager) {
        logger.info("服务单号：" + serviceManager.getServNo());
        logger.info("服务处理意见：" + serviceManager.getServDeal());
        logger.info("服务处理人:" + serviceManager.getServDealName());
        boolean flag = serviceService.dealService(serviceManager);
        if (flag) {
            return ResultDto.DEAL_SERVICE_SUCCESS;
        } else {
            return ResultDto.DEAL_SERVICE_FAILURE;
        }
    }
    @PostMapping("/serviceResult.do")
    public ResultDto serviceResult(ServiceManager serviceManager){
        logger.info("服务单号："+serviceManager.getServNo());
        logger.info("反馈结果："+serviceManager.getServResult());
        boolean flag=serviceService.resultService(serviceManager);
        if (flag) {
            return ResultDto.RESULT_SERVICE_SUCCESS;
        } else {
            return ResultDto.RESULT_SERVICE_FAILURE;
        }
    }
    @PostMapping("/serviceFeedback.do")
    public ResultDto serviceFeedback(ServiceManager serviceManager){
        logger.info("服务单号："+serviceManager.getServNo());
        boolean flag = serviceService.serviceFeedback(serviceManager);
        if (flag) {
            return ResultDto.Feedback_SERVICE_SUCCESS;
        } else {
            return ResultDto.Feedback_SERVICE_FAILURE;
        }
    }
    @GetMapping("/serviceType.do")
    public List<Integer> serviceType(){
        return serviceService.serviceType();
    }
    @GetMapping("/servicecCont")
    public Integer  servicecCont(){
        ServiceVo serviceVo = new ServiceVo();
        serviceVo.setServStatus(1);
        serviceVo.setPage(1);
        serviceVo.setLimit(20);
        DataGridView d=serviceService.selectServiceByServiceVo(serviceVo);
        List<ServiceManager> list = (ArrayList)d.getData();
        return list.size();
    }
}
